home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / lisp-mnt.el.z / lisp-mnt.el
Encoding:
Text File  |  1998-05-21  |  17.7 KB  |  577 lines

  1. ;;; lisp-mnt.el --- minor mode for Emacs Lisp maintainers
  2.  
  3. ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  6. ;; Maintainer: Eric S. Raymond <esr@snark.thyrsus.com>
  7. ;; Created: 14 Jul 1992
  8. ;; Version: $Id: lisp-mnt.el,v 1.17 1996/02/08 04:13:11 rms Exp $
  9. ;; Keywords: docs
  10. ;; X-Modified-by: Bob Weiner <weiner@mot.com>, 4/14/95, to support InfoDock
  11. ;;     headers.
  12. ;; X-Bogus-Bureaucratic-Cruft: Gruad will get you if you don't watch out!
  13.  
  14. ;; This file is part of XEmacs.
  15.  
  16. ;; XEmacs is free software; you can redistribute it and/or modify it
  17. ;; under the terms of the GNU General Public License as published by
  18. ;; the Free Software Foundation; either version 2, or (at your option)
  19. ;; any later version.
  20.  
  21. ;; XEmacs is distributed in the hope that it will be useful, but
  22. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24. ;; General Public License for more details.
  25.  
  26. ;; You should have received a copy of the GNU General Public License
  27. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  28. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  29. ;; 02111-1307, USA.
  30.  
  31. ;;; Synched up with: FSF 19.34.
  32.  
  33. ;;; Commentary:
  34.  
  35. ;; This minor mode adds some services to Emacs-Lisp editing mode.
  36. ;;
  37. ;; First, it knows about the header conventions for library packages.
  38. ;; One entry point supports generating synopses from a library directory.
  39. ;; Another can be used to check for missing headers in library files.
  40. ;; 
  41. ;; Another entry point automatically addresses bug mail to a package's
  42. ;; maintainer or author.
  43.  
  44. ;; This file can be loaded by your lisp-mode-hook.  Have it (require 'lisp-mnt)
  45.  
  46. ;; This file is an example of the header conventions.  Note the following
  47. ;; features:
  48. ;; 
  49. ;;    * Header line --- makes it possible to extract a one-line summary of
  50. ;; the package's uses automatically for use in library synopses, KWIC
  51. ;; indexes and the like.
  52. ;; 
  53. ;;    Format is three semicolons, followed by the filename, followed by
  54. ;; three dashes, followed by the summary.  All fields space-separated.
  55. ;; 
  56. ;;    * Author line --- contains the name and net address of at least
  57. ;; the principal author.
  58. ;; 
  59. ;;    If there are multiple authors, they should be listed on continuation
  60. ;; lines led by ;;<TAB>, like this:
  61. ;; 
  62. ;; ;; Author: Ashwin Ram <Ram-Ashwin@cs.yale.edu>
  63. ;; ;;    Dave Sill <de5@ornl.gov>
  64. ;; ;;    David Lawrence <tale@pawl.rpi.edu>
  65. ;; ;;    Noah Friedman <friedman@ai.mit.edu>
  66. ;; ;;    Joe Wells <jbw@maverick.uswest.com>
  67. ;; ;;    Dave Brennan <brennan@hal.com>
  68. ;; ;;    Eric Raymond <esr@snark.thyrsus.com>
  69. ;; 
  70. ;; This field may have some special values; notably "FSF", meaning
  71. ;; "Free Software Foundation".
  72. ;; 
  73. ;;    * Maintainer line --- should be a single name/address as in the Author
  74. ;; line, or an address only, or the string "FSF".  If there is no maintainer
  75. ;; line, the person(s) in the Author field are presumed to be it.  The example
  76. ;; in this file is mildly bogus because the maintainer line is redundant.
  77. ;;    The idea behind these two fields is to be able to write a Lisp function
  78. ;; that does "send mail to the author" without having to mine the name out by
  79. ;; hand. Please be careful about surrounding the network address with <> if
  80. ;; there's also a name in the field.
  81. ;; 
  82. ;;    * Created line --- optional, gives the original creation date of the
  83. ;; file.  For historical interest, basically.
  84. ;; 
  85. ;;    * Version line --- intended to give the reader a clue if they're looking
  86. ;; at a different version of the file than the one they're accustomed to.  This
  87. ;; may be an RCS or SCCS header.
  88. ;; 
  89. ;;    * Adapted-By line --- this is for FSF's internal use.  The person named
  90. ;; in this field was the one responsible for installing and adapting the
  91. ;; package for the distribution.  (This file doesn't have one because the
  92. ;; author *is* one of the maintainers.)
  93. ;; 
  94. ;;    * Keywords line --- used by the finder code (now under construction)
  95. ;; for finding Emacs Lisp code related to a topic.
  96. ;;
  97. ;;    * X-Bogus-Bureaucratic-Cruft line --- this is a joke and an example
  98. ;; of a comment header.  Headers starting with `X-' should never be used
  99. ;; for any real purpose; this is the way to safely add random headers
  100. ;; without invoking the wrath of any program.
  101. ;;
  102. ;;    * Commentary line --- enables Lisp code to find the developer's and
  103. ;; maintainers' explanations of the package internals.
  104. ;; 
  105. ;;    * Change log line --- optional, exists to terminate the commentary
  106. ;; section and start a change-log part, if one exists.
  107. ;; 
  108. ;;    * Code line --- exists so Lisp can know where commentary and/or
  109. ;; change-log sections end.
  110. ;; 
  111. ;;    * Footer line --- marks end-of-file so it can be distinguished from
  112. ;; an expanded formfeed or the results of truncation.
  113.  
  114. ;;; Change Log:
  115.  
  116. ;; Tue Jul 14 23:44:17 1992    ESR
  117. ;;    * Created.
  118.  
  119. ;;; Code:
  120.  
  121. (require 'picture)        ; provides move-to-column-force
  122. (require 'emacsbug)
  123.  
  124. ;;; Variables:
  125.  
  126. (defvar lm-header-prefix "^;;*[ \t]+\\(@\(#\)\\)?[ \t]*\\([\$]\\)?"
  127.   "Prefix that is ignored before the tag.
  128. For example, you can write the 1st line synopsis string and headers like this
  129. in your Lisp package:
  130.  
  131.    ;; @(#) package.el -- pacakge description
  132.    ;;
  133.    ;; @(#) $Maintainer:   Person Foo Bar $
  134.  
  135. The @(#) construct is used by unix what(1) and
  136. then $identifier: doc string $ is used by GNU ident(1)")
  137.  
  138. (defvar lm-comment-column 16
  139.   "Column used for placing formatted output.")
  140.  
  141. (defvar lm-commentary-header "Commentary\\|Documentation"
  142.   "Regexp which matches start of documentation section.")
  143.  
  144. (defvar lm-history-header "Change Log\\|History"
  145.   "Regexp which matches the start of code log section.")
  146.  
  147. ;;; Functions:
  148.  
  149. ;; These functions all parse the headers of the current buffer
  150.  
  151. (defsubst lm-get-header-re (header &optional mode)
  152.   "Returns regexp for matching HEADER.
  153. If called with optional MODE and with value `section',
  154. return section regexp instead."
  155.   (cond ((eq mode 'section)
  156.      (concat "^;;;;* " header ":[ \t]*$"))
  157.     (t
  158.      (concat lm-header-prefix header ":[ \t]*"))))
  159.  
  160. (defsubst lm-get-package-name ()
  161.   "Returns package name by looking at the first line."
  162.   (save-excursion
  163.     (goto-char (point-min))
  164.     (if (and (looking-at (concat lm-header-prefix))
  165.          (progn (goto-char (match-end 0))
  166.             (looking-at "\\([^\t ]+\\)")
  167.             (match-end 1)))
  168.     (buffer-substring (match-beginning 1) (match-end 1))
  169.       )))
  170.  
  171. (defun lm-section-mark (header &optional after)
  172.   "Return the buffer location of a given section start marker.
  173. The HEADER is the section mark string to search for.
  174. If AFTER is non-nil, return the location of the next line."
  175.   (save-excursion
  176.     (let ((case-fold-search t))
  177.       (goto-char (point-min))
  178.       (if (re-search-forward (lm-get-header-re header 'section) nil t)
  179.       (progn
  180.         (beginning-of-line)
  181.         (if after (forward-line 1))
  182.         (point))
  183.     nil))))
  184.  
  185. (defsubst lm-code-mark ()
  186.   "Return the buffer location of the `Code' start marker."
  187.   (lm-section-mark "Code"))
  188.  
  189. (defsubst lm-commentary-mark ()
  190.   "Return the buffer location of the `Commentary' start marker."
  191.   (lm-section-mark lm-commentary-header))
  192.  
  193. (defsubst lm-history-mark ()
  194.   "Return the buffer location of the `History' start marker."
  195.   (lm-section-mark lm-history-header))
  196.  
  197. (defun lm-header (header)
  198.   "Return the contents of the header named HEADER."
  199.   (goto-char (point-min))
  200.   (let ((case-fold-search t))
  201.     (if (and (re-search-forward (lm-get-header-re header) (lm-code-mark) t)
  202.          ;;   RCS ident likes format "$identifier: data$"
  203.          (looking-at "\\([^$\n]+\\)")
  204.          (match-end 1))
  205.     (buffer-substring (match-beginning 1) (match-end 1))
  206.       nil)))
  207.  
  208. (defun lm-header-multiline (header)
  209.   "Return the contents of the header named HEADER, with continuation lines.
  210. The returned value is a list of strings, one per line."
  211.   (save-excursion
  212.     (goto-char (point-min))
  213.     (let ((res (lm-header header)))
  214.       (cond
  215.        (res
  216.     (setq res (list res))
  217.     (forward-line 1)
  218.  
  219.     (while (and (looking-at (concat lm-header-prefix "[\t ]+"))
  220.             (progn
  221.               (goto-char (match-end 0))
  222.               (looking-at "\\(.*\\)"))
  223.             (match-end 1))
  224.       (setq res (cons (buffer-substring
  225.                (match-beginning 1)
  226.                (match-end 1))
  227.               res))
  228.       (forward-line 1))
  229.     ))
  230.       res
  231.       )))
  232.  
  233. ;; These give us smart access to the header fields and commentary
  234.  
  235. (defun lm-summary (&optional file)
  236.   "Return the one-line summary of file FILE, or current buffer if FILE is nil."
  237.   (save-excursion
  238.     (if file
  239.     (find-file file))
  240.     (goto-char (point-min))
  241.     (prog1
  242.     (if (and
  243.          (looking-at lm-header-prefix)
  244.          (progn (goto-char (match-end 0))
  245.             (looking-at "[^ ]+[ \t]+--+[ \t]+\\(.*\\)")))
  246.         (buffer-substring (match-beginning 1) (match-end 1)))
  247.       (if file
  248.       (kill-buffer (current-buffer)))
  249.       )))
  250.  
  251. (defun lm-crack-address (x)
  252.   "Split up an email address into full name and real email address.
  253. The value is a cons of the form (FULLNAME . ADDRESS)."
  254.   (cond ((string-match "\\(.+\\) [(<]\\(\\S-+@\\S-+\\)[>)]" x)
  255.      (cons (substring x (match-beginning 1) (match-end 1))
  256.            (substring x (match-beginning 2) (match-end 2))))
  257.     ((string-match "\\(\\S-+@\\S-+\\) [(<]\\(.*\\)[>)]" x)
  258.      (cons (substring x (match-beginning 2) (match-end 2))
  259.            (substring x (match-beginning 1) (match-end 1))))
  260.     ((string-match "\\S-+@\\S-+" x)
  261.      (cons nil x))
  262.     (t
  263.      (cons x nil))))
  264.  
  265. (defun lm-authors (&optional file)
  266.   "Return the author list of file FILE, or current buffer if FILE is nil.
  267. Each element of the list is a cons; the car is the full name,
  268. the cdr is an email address."
  269.   (save-excursion
  270.     (if file
  271.     (find-file file))
  272.     ;; XEmacs change (Is E-MAIL an infodock header? -sb)
  273.     (let* ((authorlist (lm-header-multiline "author"))
  274.        (email-list (lm-header-multiline "E-MAIL"))
  275.        (authors authorlist))
  276.       (prog1
  277.       (if (null email-list)
  278.           (mapcar 'lm-crack-address authorlist)
  279.         (while (and email-list authors)
  280.           (setcar authors (cons (car authors) (car email-list)))
  281.           (setq email-list (cdr email-list)
  282.             authors (cdr authors)))
  283.         authorlist)
  284.     (if file
  285.         (kill-buffer (current-buffer))))
  286.       )))
  287.  
  288. (defun lm-maintainer (&optional file)
  289.   "Return the maintainer of file FILE, or current buffer if FILE is nil.
  290. The return value has the form (NAME . ADDRESS)."
  291.   (save-excursion
  292.     (if file
  293.     (find-file file))
  294.     (prog1
  295.     (let ((maint (lm-header "maintainer")))
  296.       (if maint
  297.           (lm-crack-address maint)
  298.         (car (lm-authors))))
  299.       (if file
  300.       (kill-buffer (current-buffer))))))
  301.  
  302. (defun lm-creation-date (&optional file)
  303.   "Return the created date given in file FILE, or current buffer if FILE is nil."
  304.   (save-excursion
  305.     (if file
  306.     (find-file file))
  307.     (prog1
  308.     ;; XEmacs change (Is ORIG-DATE an Infodock header? -sb)
  309.     (or (lm-header "created")
  310.         (let ((date-and-time (lm-header "ORIG-DATE")))
  311.           (if date-and-time
  312.           (substring date-and-time 0
  313.                  (string-match " " date-and-time)))))
  314.       (if file
  315.       (kill-buffer (current-buffer)))
  316.       )))
  317.  
  318. (defun lm-last-modified-date (&optional file)
  319.   "Return the modify-date given in file FILE, or current buffer if FILE is nil."
  320.   (save-excursion 
  321.     (if file
  322.     (find-file file))
  323.     (prog1
  324.     (if (progn
  325.           (goto-char (point-min))
  326.           (re-search-forward
  327.            "\\$Id: [^ ]+ [^ ]+ \\([^/]+\\)/\\([^/]+\\)/\\([^ ]+\\) "
  328.            (lm-code-mark) t))
  329.         (format "%s %s %s"
  330.             (buffer-substring (match-beginning 3) (match-end 3))
  331.             (nth (string-to-int 
  332.               (buffer-substring (match-beginning 2) (match-end 2)))
  333.              '("" "Jan" "Feb" "Mar" "Apr" "May" "Jun"
  334.                "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
  335.             (buffer-substring (match-beginning 1) (match-end 1)))
  336.       ;; XEmacs change (Infodock change? -sb)
  337.       (let ((date-and-time (lm-header "LAST-MOD")))
  338.         (if date-and-time
  339.         (substring date-and-time 0
  340.                (string-match " " date-and-time)))))
  341.       (if file
  342.       (kill-buffer (current-buffer)))
  343.       )))
  344.  
  345. (defun lm-version (&optional file)
  346.   "Return the version listed in file FILE, or current buffer if FILE is nil.
  347. This can befound in an RCS or SCCS header to crack it out of."
  348.   (save-excursion 
  349.     (if file
  350.     (find-file file))
  351.     (prog1
  352.     (or
  353.      (lm-header "version")
  354.      (let ((header-max (lm-code-mark)))
  355.        (goto-char (point-min))
  356.        (cond
  357.         ;; Look for an RCS header
  358.         ((re-search-forward "\\$Id: [^ ]+ \\([^ ]+\\) " header-max t)
  359.          (buffer-substring (match-beginning 1) (match-end 1)))
  360.  
  361.         ;; Look for an SCCS header
  362.         ((re-search-forward 
  363.           (concat
  364.            (regexp-quote "@(#)")
  365.            (regexp-quote (file-name-nondirectory (buffer-file-name)))
  366.            "\t\\([012345679.]*\\)")
  367.           header-max t)
  368.          (buffer-substring (match-beginning 1) (match-end 1)))
  369.  
  370.         (t nil))))
  371.       (if file
  372.       (kill-buffer (current-buffer)))
  373.       )))
  374.  
  375. (defun lm-keywords (&optional file)
  376.   "Return the keywords given in file FILE, or current buffer if FILE is nil."
  377.   (save-excursion
  378.     (if file
  379.     (find-file file))
  380.     (prog1
  381.     (let ((keywords (lm-header "keywords")))
  382.       (and keywords (downcase keywords)))
  383.       (if file
  384.       (kill-buffer (current-buffer)))
  385.       )))
  386.  
  387. (defun lm-adapted-by (&optional file)
  388.   "Return the adapted-by names in file FILE, or current buffer if FILE is nil.
  389. This is the name of the person who cleaned up this package for
  390. distribution."
  391.   (save-excursion
  392.     (if file
  393.     (find-file file))
  394.     (prog1
  395.     (lm-header "adapted-by")
  396.       (if file
  397.       (kill-buffer (current-buffer)))
  398.       )))
  399.  
  400. (defun lm-commentary (&optional file)
  401.   "Return the commentary in file FILE, or current buffer if FILE is nil.
  402. The value is returned as a string.  In the text, the commentary starts
  403. with tag `Commentary' and ends with tag `Change Log' or `History'."
  404.   (save-excursion
  405.     (if file
  406.     (find-file file))
  407.     (prog1
  408.     (let ((commentary    (lm-commentary-mark))
  409.           (change-log    (lm-history-mark))
  410.           (code        (lm-code-mark))
  411.           )
  412.       (cond
  413.        ((and commentary change-log)
  414.         (buffer-substring commentary change-log))
  415.        ((and commentary code)
  416.         (buffer-substring commentary code))
  417.        (t
  418.         ;; XEmacs change (Infodock headers? -sb)
  419.         (setq commentary (lm-section-mark "DESCRIPTION" t)
  420.           code (lm-section-mark "DESCRIP-END"))
  421.         (and commentary end (buffer-substring commentary code)))))
  422.       (if file
  423.       (kill-buffer (current-buffer)))
  424.       )))
  425.  
  426. ;;; Verification and synopses
  427.  
  428. (defun lm-insert-at-column (col &rest strings)
  429.   "Insert list of STRINGS, at column COL."
  430.    (if (> (current-column) col) (insert "\n"))
  431.    (move-to-column-force col)
  432.    (apply 'insert strings))
  433.  
  434. (defun lm-verify (&optional file showok &optional verb)
  435.   "Check that the current buffer (or FILE if given) is in proper format.
  436. If FILE is a directory, recurse on its files and generate a report in
  437. a temporary buffer."
  438.   (interactive)
  439.   (let* ((verb    (or verb (interactive-p)))
  440.      ret
  441.      name
  442.      )
  443.     (if verb
  444.     (setq ret "Ok."))               ;init value
  445.  
  446.     (if (and file (file-directory-p file))
  447.     (setq
  448.      ret
  449.      (progn
  450.        (switch-to-buffer (get-buffer-create "*lm-verify*"))
  451.        (erase-buffer)
  452.        (mapcar
  453.         '(lambda (f)
  454.            (if (string-match ".*\\.el$" f)
  455.            (let ((status (lm-verify f)))
  456.              (if status
  457.              (progn
  458.                (insert f ":")
  459.                (lm-insert-at-column lm-comment-column status "\n"))
  460.                (and showok
  461.                 (progn
  462.                   (insert f ":")
  463.                   (lm-insert-at-column lm-comment-column "OK\n")))))))
  464.         (directory-files file))
  465.        ))
  466.       (save-excursion
  467.     (if file
  468.         (find-file file))
  469.     (setq name (lm-get-package-name))
  470.  
  471.     (setq
  472.      ret
  473.      (prog1
  474.          (cond
  475.           ((null name)
  476.            "Can't find a package NAME")
  477.  
  478.           ((not (lm-authors))
  479.            "Author: tag missing.")
  480.  
  481.           ((not (lm-maintainer))
  482.            "Maintainer: tag missing.")
  483.  
  484.           ((not (lm-summary))
  485.            "Can't find a one-line 'Summary' description")
  486.  
  487.           ((not (lm-keywords))
  488.            "Keywords: tag missing.")
  489.  
  490.           ((not (lm-commentary-mark))
  491.            "Can't find a 'Commentary' section marker.")
  492.  
  493.           ((not (lm-history-mark))
  494.            "Can't find a 'History' section marker.")
  495.  
  496.           ((not (lm-code-mark))
  497.            "Can't find a 'Code' section marker")
  498.  
  499.           ((progn
  500.          (goto-char (point-max))
  501.          (not
  502.           (re-search-backward
  503.            (concat "^;;;[ \t]+" name "[ \t]+ends here[ \t]*$"
  504.                "\\|^;;;[ \t]+ End of file[ \t]+" name)
  505.            nil t
  506.            )))
  507.            (format "Can't find a footer line for [%s]" name))
  508.           (t
  509.            ret))
  510.        (if file
  511.            (kill-buffer (current-buffer)))
  512.        ))))
  513.     (if verb
  514.     (message ret))
  515.     ret
  516.     ))
  517.  
  518. (defun lm-synopsis (&optional file showall)
  519.   "Generate a synopsis listing for the buffer or the given FILE if given.
  520. If FILE is a directory, recurse on its files and generate a report in
  521. a temporary buffer.  If SHOWALL is non-nil, also generate a line for files
  522. which do not include a recognizable synopsis."
  523.   (interactive
  524.    (list
  525.     (read-file-name "Synopsis for (file or dir): ")))
  526.  
  527.   (if (and file (file-directory-p file))
  528.       (progn
  529.     (switch-to-buffer (get-buffer-create "*lm-verify*"))
  530.     (erase-buffer)
  531.     (mapcar
  532.       '(lambda (f)
  533.         (if (string-match ".*\\.el$" f)
  534.         (let ((syn (lm-synopsis f)))
  535.           (if syn
  536.               (progn
  537.             (insert f ":")
  538.             (lm-insert-at-column lm-comment-column syn "\n"))
  539.             (and showall
  540.              (progn
  541.                (insert f ":")
  542.                (lm-insert-at-column lm-comment-column "NA\n")))))))
  543.       (directory-files file))
  544.     )
  545.     (save-excursion
  546.       (if file
  547.       (find-file file))
  548.       (prog1
  549.       (lm-summary)
  550.     (if file
  551.         (kill-buffer (current-buffer)))
  552.     ))))
  553.  
  554. (defun lm-report-bug (topic)
  555.   "Report a bug in the package currently being visited to its maintainer.
  556. Prompts for bug subject.  Leaves you in a mail buffer."
  557.   (interactive "sBug Subject: ")
  558.   (let ((package    (lm-get-package-name))
  559.     (addr        (lm-maintainer))
  560.     (version    (lm-version)))
  561.     (mail nil
  562.       (if addr
  563.           (concat (car addr) " <" (cdr addr) ">")
  564.         bug-gnu-emacs)
  565.       topic)
  566.     (goto-char (point-max))
  567.     (insert "\nIn "
  568.         package
  569.         (if version (concat " version " version) "")
  570.         "\n\n")
  571.     (message
  572.      (substitute-command-keys "Type \\[mail-send] to send bug report."))))
  573.  
  574. (provide 'lisp-mnt)
  575.  
  576. ;;; lisp-mnt.el ends here
  577.